<template>
  <div class="wrapper text-center registerPage" >
    <div class="p-2">
      <div class="re_cell d-flex" style="height: 40px;line-height: 40px;">
        <label class="w110 mr-1" ><i class="iconfont icon-B66 font20"></i>系统名称</label>
        <el-input v-model="user.sysName"></el-input>
      </div>
      <div class="re_cell d-flex" style="height: 40px;line-height: 40px;">
        <label class="w110 mr-1" ><i class="iconfont icon-setting font20"></i>系统端口</label>
        <el-input  v-model="user.port" @blur="validatePort"></el-input>
      </div>
      <template v-if="tencentId==1">
        <div class="re_cell d-flex  align-items-center">
          <label class="w110 mr-1"><i class="iconfont icon-user font20"></i>用户名</label>
          <el-input  v-model="user.name"></el-input>
        </div>
        <div class="re_cell d-flex  align-items-center">
          <label class="w110 mr-1"><i class="iconfont icon-mima font20 font-weight-bold"></i>密码</label>
          <el-input v-model="user.pwd"></el-input>
        </div>
      </template>
     <!-- <template v-else-if="tencentId==2">
        <div class="re_cell d-flex  align-items-center">
          <label class="w110 mr-1"><i class="iconfont icon-user font20"></i>手机号</label>
          <el-input v-model="user.phoneNo"></el-input>
        </div>
      </template>-->
      <el-button class="ur-btn ur-btn-primary w-100 re_cell font20" style="height: 50px;" @click="save">
        <i class="iconfont icon-chenggong font20" style="margin-right: 6px;"></i><label class="font18">保存</label>
      </el-button>
    </div>
  </div>
</template>

<script>
  import {mapState} from 'vuex'

  export default {
    components: {},
    props: {},
    data() {
      return {
        user:{},
        tencentId:1//1:照明一网统管 2：瑞斯康智能空开
      }
    },
    watch: {
    },
    computed: {
      ...mapState({
        openid:(state)=>state.user.openid
      })
    },
    methods: {
      validatePort(){
        let tencentId = window.location.hash.split('/')[3]||sessionStorage.getItem('tencentId')
        let sysList=this.global.SysList[tencentId]
        let res=false
        let resTxt=''
        for(let i=0;i<sysList.length;i++){
          if(sysList[i].port==this.user.port) res=true
        }
        if (!res) {
          this.$weui.topTips('端口不正确！');
          resTxt='端口不正确'
        }
        return resTxt
      },
      validateForm(){
        let {sysName,port,name,pwd,phoneNo}=this.user
        let errStr=''
        if(!sysName||sysName==''){
          errStr+='系统名称必填'
        }
        if(!port||port==''){
          errStr+='</br>系统端口必填'
        }else{
          errStr+=this.validatePort()
        }
        if(this.tencentId==1){
          if(!name||name==''){
            errStr+='</br>用户名必填'
          }
          if(!pwd||pwd==''){
            errStr+='</br>用户密码必填'
          }
        }
      /*  else if(this.tencentId==2){
          var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
          if(!reg_tel.test((phoneNo||'').trim())){
            errStr+='</br>请填写正确的手机号码！'
          }
        }*/
        return errStr
      },
      save(){
        let _this=this
        let {sysName,port,name,pwd,phoneNo}=this.user
        let errStr=this.validateForm()
        if(errStr){
          if(errStr.substr(0,5)=='</br>'){
            errStr=errStr.slice(5)
          }
          return this.$weui.topTips(errStr)
        }else{
          if(_this.tencentId==1){
            this.$store.commit('comm/setSysList',{label:sysName, value:port,name:name,pwd:pwd})
          }else if(_this.tencentId==2){
            this.$store.commit('comm/setSysList',{label:sysName, value:port,phoneNo:phoneNo})
          }
          this.$weui.toast('操作成功', {
            duration: 2000,
             callback: function(){
              _this.$router.go(-1)
            }
          });
        }
      },
    },
    created() {
      let tencentId = sessionStorage.getItem('tencentId')
      this.tencentId = tencentId
      let sysInitDefault=this.global.SysList[tencentId][0]
      this.$set(this.user,'sysName',sysInitDefault.name)
      this.$set(this.user,'port',sysInitDefault.port)
    },
    mounted() {
    }
  }
</script>
<style scoped lang="stylus" rel="stylesheet/stylus">
  .iconfont{margin-right:4px;}
  .registerPage{
    .re_cell{margin-top:30px;}
  }
</style>
